<?php
class PeriodicExpenditureTypesController extends AppController {
    var $name = 'PeriodicExpenditureTypes';

    function index() {
        $this->PeriodicExpenditureType->recursive = 0;
/*		 $permit  = $this->Session->read('permit_build'); 
		if ( $permit!= null ){
            $data = null;
            $new  = null;
            foreach ($permit as $key => $value) {
				$this->paginate = array(
                             'conditions' => array( 'PeriodicExpenditureType.building_id' => $value )
                         );  
				$data[]  = $this->paginate();
			}   
            foreach ( $data as $key1 => $value1)
			{   
				if ($value1 != null)
				{
					foreach ( $value1 as $key2 => $value2)
					{	
						 $new[] = $value1[$key2];
					}
				}
			}
            $this->set('periodicExpenditureTypes', $new);
        }else{
              $this->Session->setFlash(__('You have nothing assigned', true));
              $this->redirect(array('controller' => 'users', 'action' => 'welcome'));
        }   
 */
	    $this->set('periodicExpenditureTypes', $this->paginate());
	}

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid periodic expenditure type', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('periodicExpenditureType', $this->PeriodicExpenditureType->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->PeriodicExpenditureType->create();
            if ($this->PeriodicExpenditureType->save($this->data)) {
                $this->Session->setFlash(__('The periodic expenditure type has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The periodic expenditure type could not be saved. Please, try again.', true));
            }
        }
        $expenditureTypes = $this->PeriodicExpenditureType->ExpenditureType->find('list');
        $buildings = $this->PeriodicExpenditureType->Building->find('list');
        $amountTypes = $this->PeriodicExpenditureType->AmountType->find('list',
                array('conditions' => array('AmountType.lookup_type' => 'PERIODIC_EXPENDITURE_TYPE')));
        $periodTypes = $this->PeriodicExpenditureType->PeriodType->find('list',
                array('conditions' => array('PeriodType.lookup_type' => 'PERIOD')));
        $this->set(compact('expenditureTypes', 'buildings', 'amountTypes', 'periodTypes'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid periodic expenditure type', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->PeriodicExpenditureType->save($this->data)) {
                $this->Session->setFlash(__('The periodic expenditure type has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The periodic expenditure type could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->PeriodicExpenditureType->read(null, $id);
        }
        $expenditureTypes = $this->PeriodicExpenditureType->ExpenditureType->find('list');
        $buildings = $this->PeriodicExpenditureType->Building->find('list');
        $amountTypes = $this->PeriodicExpenditureType->AmountType->find('list',
                array('conditions' => array('AmountType.lookup_type' => 'PERIODIC_EXPENDITURE_TYPE')));
        $periodTypes = $this->PeriodicExpenditureType->PeriodType->find('list',
                array('conditions' => array('PeriodType.lookup_type' => 'PERIOD')));
        $this->set(compact('expenditureTypes', 'buildings', 'amountTypes', 'periodTypes'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for periodic expenditure type', true));
            $this->redirect(array('action'=>'index'));
        }
        if ($this->PeriodicExpenditureType->delete($id)) {
            $this->Session->setFlash(__('Periodic expenditure type deleted', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Session->setFlash(__('Periodic expenditure type was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }
}
?>
